import os
import re
import threading

import numpy as np
import time
from commen.log import Logger
import openpyxl
import commen.config
def ImuDatacontinuity():
    #---------读取config文件获取筛选字段内容--------------------------
    mcukey = commen.config.global_config.getRaw('keywords', 'mcurtk')
    mpurtk = commen.config.global_config.getRaw('keywords', 'mpurtk')
    # ---------读取config文件获取筛选字段内容--------------------------
    #-----------------------创建excel--------------------------------------------
    # 获取当前时间
    time_now = time.strftime("%Y%m%d-%H%M", time.localtime())
    # 获取当前路径
    result_root = os.getcwd()
    root = os.getcwd()
    result_path = result_root + r'\result' + "\\" + time_now
    log_path = root + r'\original-logfile' + '\\'
    # -----遍历对比log路径获取要对比的文件名路径信息----------------------
    for root, dirs, files in os.walk(log_path):
        for file in files:
            if "Data" in file:
                aa = os.path.join(root, file)
            if "mcu" in file:
                bb = os.path.join(root, file)
    # -----遍历对比log路径获取要对比的文件名路径信息----------------------
    book = openpyxl.Workbook()  # 打开一个将写的文件
    sheet = book.create_sheet(index=0)  # 在将写的文件创建sheet
    saveExcel = result_path+"can到MPU的rtk数据正确性验证结果.xlsx"
    sheet.cell(1, 2).value = "mcurtk数据系统时间"
    sheet.cell(1, 3).value = "mpurtk数据系统时间"
    sheet.cell(1, 4).value = "mcurtk数据"
    sheet.cell(1, 5).value = "MPUrtk数据"
    sheet.cell(1, 6).value = "结果"
    mcurtklist = []
    mpurtklist = []
    mcurtksystemtimelist = []
    mpurtksystemtimelist = []

    row =1
    #-----------------------创建excel--------------------------------------------

    logger  = Logger(logger='TestMylog').getlog()
    try:
        mpufm = open(aa, 'r', encoding='UTF-8')  # 打开目标文件
        mcufm = open(bb, 'r', encoding='UTF-8') # 打开目标文件
    except:
        mpufm = open(aa, 'r', encoding='gbk')  # 打开目标文件
        mcufm = open(bb, 'r', encoding='gbk')  # 打开目标文件
    logger.info("打开目标文件")
    mculines = mcufm.readlines()
    mpulines = mpufm.readlines()
    logger.info("获取内容")
    logger.info("开始逐行读取")
    for rtkline in mculines:
        if mcukey in rtkline:
            mcurtksystemtime = rtkline[:rtkline.index("<thread")]
            mcurtkold = rtkline.split("+0:")
            mcurtk = mcurtkold[1].replace("+10:","")
            mcurtk = mcurtk.replace("+20:", "")
            mcurtk = mcurtk.replace("+30:", "")
            mcurtk = mcurtk.replace(")", "")
            mcurtk = mcurtk.replace("-", "")
            mcurtk = mcurtk.replace(" ", "")
            mcurtk1 = mcurtk[6:22]
            int(mcurtk1[:2] , 16)
            mcurtk1_vail =int(mcurtk1[:2] , 16) & 0x01
            mcurtk1_len = (int(mcurtk1[:2] , 16)&0x0e) >>1
            if mcurtk1_vail == 1:
                if mcurtk1_len == 0:
                    mcurtk1 = ""
                if mcurtk1_len == 1:
                    mcurtk1 = mcurtk1[2:4]
                if mcurtk1_len == 2:
                    mcurtk1 = mcurtk1[2:6]
                if mcurtk1_len == 3:
                    mcurtk1 = mcurtk1[2:8]
                if mcurtk1_len == 4:
                    mcurtk1 = mcurtk1[2:10]
                if mcurtk1_len == 5:
                    mcurtk1 = mcurtk1[2:12]
                if mcurtk1_len == 6:
                    mcurtk1 = mcurtk1[2:14]
                if mcurtk1_len == 7:
                    mcurtk1 = mcurtk1[2:16]

            else:
                mcurtk1=""
            mcurtk2 = mcurtk[22:38]
            mcurtk2_vail = int(mcurtk2[:2] , 16) & 0x01
            mcurtk2_len = (int(mcurtk2[:2] , 16)& 0x0e) >> 1
            if mcurtk2_vail == 1:
                if mcurtk2_len == 0:
                    mcurtk2 =""
                if mcurtk2_len == 1:
                    mcurtk2 = mcurtk2[2:4]
                if mcurtk2_len == 2:
                    mcurtk2 = mcurtk2[2:6]
                if mcurtk2_len == 3:
                    mcurtk2 = mcurtk2[2:8]
                if mcurtk2_len == 4:
                    mcurtk2 = mcurtk2[2:10]
                if mcurtk2_len == 5:
                    mcurtk2 = mcurtk2[2:12]
                if mcurtk2_len == 6:
                    mcurtk2 = mcurtk2[2:14]
                if mcurtk2_len == 7:
                    mcurtk2 = mcurtk2[2:16]

            else:
                mcurtk2=""
            mcurtk3 = mcurtk[38:54]
            mcurtk3_vail = int(mcurtk3[:2] , 16) & 0x01
            mcurtk3_len = (int(mcurtk3[:2] , 16) & 0x0e) >> 1
            if mcurtk3_vail == 1:
                if mcurtk3_len == 0:
                    mcurtk3 = ""
                if mcurtk3_len == 1:
                    mcurtk3 = mcurtk3[2:4]
                if mcurtk3_len == 2:
                    mcurtk3 = mcurtk3[2:6]
                if mcurtk3_len == 3:
                    mcurtk3 = mcurtk3[2:8]
                if mcurtk3_len == 4:
                    mcurtk3 = mcurtk3[2:10]
                if mcurtk3_len == 5:
                    mcurtk3 = mcurtk3[2:12]
                if mcurtk3_len == 6:
                    mcurtk3 = mcurtk3[2:14]
                if mcurtk3_len == 7:
                    mcurtk3 = mcurtk3[2:16]

            else:
                mcurtk3=""
            mcurtk4 = mcurtk[54:70]
            mcurtk4_vail = int(mcurtk4[:2] , 16) & 0x01
            mcurtk4_len = (int(mcurtk4[:2] , 16) & 0x0e) >> 1
            if mcurtk4_vail == 1:
                if mcurtk4_len == 0:
                    mcurtk4 = ""
                if mcurtk4_len == 1:
                    mcurtk4 = mcurtk4[2:4]
                if mcurtk4_len == 2:
                    mcurtk4 = mcurtk4[2:6]
                if mcurtk4_len == 3:
                    mcurtk4 = mcurtk4[2:8]
                if mcurtk4_len == 4:
                    mcurtk4 = mcurtk4[2:10]
                if mcurtk4_len == 5:
                    mcurtk4 = mcurtk4[2:12]
                if mcurtk4_len == 6:
                    mcurtk4 = mcurtk4[2:14]
                if mcurtk4_len == 7:
                    mcurtk4 = mcurtk4[2:16]

            else:
                mcurtk4=""
            message = mcurtk1+mcurtk2+mcurtk3+mcurtk4
            mcurtklist.append(message)
            mcurtksystemtimelist.append(mcurtksystemtime)
    for rtk in mpulines:
        if mpurtk in rtk:
            mpurtksystemtime = rtk[:rtk.index("<thread")]
            mpurtksystemtimelist.append(mpurtksystemtime)
            message = rtk.split("buf :")
            message = message[1].rstrip()
            message = message.lstrip()
            mpurtklist.append(message)
    for i in range(len(mcurtklist)):
        row = row+1
        logger.info("开始查找数据")
        if mcurtklist[i] in mpurtklist:
            sheet.cell(row, 2).value = mcurtksystemtimelist[i]
            sheet.cell(row, 3).value = mpurtksystemtimelist[mpurtklist.index(mcurtklist[i])]
            sheet.cell(row, 4).value = mcurtklist[i]
            sheet.cell(row, 5).value = mcurtklist[i]
            sheet.cell(row, 6).value = "一致"
        else:
            sheet.cell(row, 2).value = mcurtksystemtimelist[i]
            sheet.cell(row, 4).value = mcurtklist[i]
            sheet.cell(row, 6).value = "丢包"


    book.save(saveExcel)

ImuDatacontinuity()